
//
//  ClassViewController.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/16.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "ClassViewController.h"
#import "ClassModelView.h"
#import "classModel/ClassModel.h"
#import "ClassListController.h"

@interface ClassViewController()<UITableViewDelegate, UITableViewDataSource>{
    
    UITableView *_tableView;
    
    BOOL close[100];
}

@property (nonatomic,strong) NSArray *sourceArr;
@property (nonatomic,strong) NSArray *jieArr;

@end

@implementation ClassViewController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    [self setNavBarTitle:@"课程分类"];
    
    [self creatTableViewAndGetData];
}


- (void)creatTableViewAndGetData{
    
    ClassModelView *classModel = [[ClassModelView alloc]init];
    
    [classModel getClassData];
    
    classModel.succeedBlock = ^(id succeedValue, id succeedJieValue){
        
        NSLog(@"succeedValue == %@  %@", succeedValue, succeedJieValue);
        
        _sourceArr = succeedValue;
        _jieArr = succeedJieValue;
        
        
        /**
         *  这个的目的是为了启动APP，单元格是收起
         */
        for (int i = 0 ; i < _sourceArr.count; i++) {
            
            close[i] = NO;
        }
        
        [_tableView reloadData];
    };
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SIZEWIDTH, SIZEHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _sourceArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (close[section]) {
        
        return 0;
    }
    
    NSArray *array = _jieArr[section];
    
    return array.count;
}

//组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ident = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ident];
    }
    
    ClassModel *classModel = _jieArr[indexPath.section][indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@",classModel.xsm];
    cell.textLabel.font = [UIFont systemFontOfSize:ScaleSize(36)];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",classModel.count];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:ScaleSize(36)];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        ClassModel *model = _jieArr[indexPath.section][indexPath.row];
    
    ClassListController *list = [[ClassListController alloc]initDataWithServerData:model.yxid andXsm:model.xsm];
    [self navigationCanDragBack:YES];
    
    list.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:list animated:YES];
    
}

//创建组头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIControl *view = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, SIZEWIDTH, 50)];
    view.tag = 1000 + section;    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [view addTarget:self action:@selector(sectionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 30)];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:14];
    
    ClassModel *classModel = _sourceArr[section];
    
    label.text = [NSString stringWithFormat:@"%@",classModel.xsm];
    [view addSubview:label];
    
    
    return view;
    
}

-(void)sectionClick:(UIControl *)view{
    
    //获取点击的组
    NSInteger i = view.tag - 1000;
    //取反
    close[i] = !close[i];
    //刷新列表
    NSIndexSet * index = [NSIndexSet indexSetWithIndex:i];
    [_tableView reloadSections:index withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSArray *)sourceArr{
    
    if (_sourceArr) {
        
        _sourceArr = [NSArray array];
    }
    return _sourceArr;
}
- (NSArray *)jieArr{
    
    if (_jieArr) {
        
        _jieArr = [NSArray array];
    }
    return _jieArr;
}

@end
